import React, { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";
//定义高阶组件处理路由守卫
function WithRouter(Cpn: any) {
  function NewCpn() {
    const { pathname } = useLocation();
    const navigate = useNavigate();
    const userInfo = JSON.parse(localStorage.getItem("userinfor") || "{}");
    useEffect(() => {
      if (!userInfo.nickname && !["/login", "/"].includes(pathname)) {
        navigate("/login");
      }
    }, [pathname]);
    return <Cpn />;
  }
  return NewCpn;
}
                                                                                                                      
export default WithRouter;
